import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import Main from "../components/main.vue";
let  Modules=import.meta.glob("../views/*.vue")

const router = createRouter({
  history: createWebHistory(),
  // history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect:'/login',
    },
    {
      path: '/index',
      component:Main,
      children:[
        {
          path:'',
          name:'Index',
          component:()=>import('../views/index.vue'),
          meta:{
            keepAlive:false
          }
        },
      ]
    },
   
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/login.vue')
    },
    // {
    //   path: '/system',
    //   name: 'System',
    //   redirect:'/system/user',
    //   component:Main,
    //   children:[
    //   ]
    // },
    {
      path: '/:catchAll(.*)',
      component: ()=>import('../views/404.vue')
    }
  ]
})

function addRotesFunc(){
  let  menu=JSON.parse(sessionStorage.getItem("menu"))
  if(!menu) return
  let children=menu.map(item=>{
    return {
      path:item.path,
      component: Modules[`${item.description}`],
      meta:{
        keepAlive:false
      }
    }
  })
  router.addRoute({
    path: '/system',
    name: 'System',
    component:()=>import('../components/main.vue'),
    children:[...children]
  })
  
  router.addRoute(
    {
      path: '/*',
      component: ()=>import('../views/404.vue')
    }
  )
}
addRotesFunc()
export default router
